package com.liu.mybatisPlus.q.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.mybatisPlus.q.entity.base.QGroupUserOperationQuantity;
import com.liu.mybatisPlus.q.mapper.QGroupUserOperationQuantityMapper;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2025-04-01
 */
@Service
public class QGroupUserOperationQuantityServiceImpl extends ServiceImpl<QGroupUserOperationQuantityMapper, QGroupUserOperationQuantity> implements QGroupUserOperationQuantityService {

    @Override
    public void statisticsOperationQuantityByType(Long groupId, Long userId, OperationTypeEnum operationTypeEnum) {
        DateTime today = DateUtil.offsetHour(DateUtil.date(), 8);
        QueryWrapper<QGroupUserOperationQuantity> qGroupUserOperationQuantityQueryWrapper = new QueryWrapper<>();
        qGroupUserOperationQuantityQueryWrapper.eq("group_id", groupId);
        qGroupUserOperationQuantityQueryWrapper.eq("user_id", userId);
        qGroupUserOperationQuantityQueryWrapper.eq("operation_type", operationTypeEnum.getOperationType());
        qGroupUserOperationQuantityQueryWrapper.eq("statistics_date", DateUtil.format(today, "yyyy-MM-dd"));
        QGroupUserOperationQuantity qGroupUserOperationQuantity = super.getOne(qGroupUserOperationQuantityQueryWrapper);
        if (qGroupUserOperationQuantity == null) {
            qGroupUserOperationQuantity = new QGroupUserOperationQuantity();
            qGroupUserOperationQuantity.setGroupId(groupId);
            qGroupUserOperationQuantity.setUserId(userId);
            qGroupUserOperationQuantity.setOperationType(operationTypeEnum.getOperationType());
            qGroupUserOperationQuantity.setOperationName(operationTypeEnum.getOperationName());
            qGroupUserOperationQuantity.setQuantity(1);
            qGroupUserOperationQuantity.setStatisticsDate(today);
            super.save(qGroupUserOperationQuantity);
        } else {
            qGroupUserOperationQuantity.setQuantity(qGroupUserOperationQuantity.getQuantity() + 1);
            super.updateById(qGroupUserOperationQuantity);
        }
    }

}
